﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
//
using Mal.Balades.Admin.Services;


namespace Mal.Balades.Admin.ViewModels.VisitViewModels.Dashboard
{
    public class VisitsDashBoardViewModel : PageViewModelBase
    {
        #region Fields

        IAdminService service;

        private ObservableCollection<VisitDashboardViewModel> visits;
        private PagedCollectionView visitsToDisplay;

        private bool isLoading;
        private string searchedText;

        private RelayCommand addCommand;

        private ObservableCollection<DashboardFilterViewModel> filters;

        private DashboardFilterViewModel selectedFilter;


        #endregion

        #region Construction / Destruction
        public VisitsDashBoardViewModel()
        //: base(mainViewModel)
        {
            this.service = AdminServiceFactory.GetInstance();

            this.service.GetVisitsForDashboardCompleted += new ServiceResponse<ReadOnlyCollection<IVisitDashboard>>(OnGetVisitsForDashboardCompleted);
            LoadVisitsForDashBoard();
        }

       



        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public ObservableCollection<VisitDashboardViewModel> Visits
        {
            get { return visits; }
            set
            {
                if (visits != value)
                {
                    visits = value;
                    RaisePropertyChanged("Visits");
                }
            }
        }

        public PagedCollectionView VisitsToDisplay
        {
            get { return visitsToDisplay; }
            set
            {
                if (visitsToDisplay != value)
                {
                    visitsToDisplay = value;
                    RaisePropertyChanged("VisitsToDisplay");
                }
            }
        }

        public bool IsLoading
        {
            get { return isLoading; }
            set
            {
                if (isLoading != value)
                {
                    isLoading = value;
                    RaisePropertyChanged("IsLoading");

                    if (isLoading)
                    {
                        Messenger.Default.Send<MainViewModel.MessageBarMessage>(
                            new MainViewModel.MessageBarMessage(this, "Chargement des visites en cours ..."));
                    }
                    else
                    {
                        Messenger.Default.Send<MainViewModel.MessageBarMessage>(
                           new MainViewModel.MessageBarMessage(this));
                    }
                }
            }
        }

        public DashboardFilterViewModel SelectedFilter
        {
            get { return selectedFilter; }
            set
            {
                if (selectedFilter != value)
                {
                    selectedFilter = value;
                    RaisePropertyChanged("SelectedFilter");
                    FilterVisits();
                }
            }
        }

        public ObservableCollection<DashboardFilterViewModel> Filters
        {
            get { return filters; }
            set
            {
                if (filters != value)
                {
                    filters = value;
                    RaisePropertyChanged("Filters");
                }
            }
        }

        public string SearchedText
        {
            get { return searchedText; }
            set
            {
                if (searchedText != value)
                {
                    searchedText = value;
                    RaisePropertyChanged("SearchedText");
                    //RaisePropertyChanged("VisitsToDisplay");
                    FilterVisits();
                }
            }
        }

        public ICommand AddCommand
        {
            get
            {
                if (this.addCommand == null)
                {
                    this.addCommand = new RelayCommand(OnAdd);
                }
                return this.addCommand;
            }
        }


        #endregion

        #region Methods
        public override void Cleanup()
        {
            if (this.service != null)
            {
                this.service.GetVisitsForDashboardCompleted -= new ServiceResponse<ReadOnlyCollection<IVisitDashboard>>(OnGetVisitsForDashboardCompleted);

                this.service.Cleanup();
                this.service = null;
            }
            base.Cleanup();
        }

        private void LoadVisitsForDashBoard()
        {
            IsLoading = true;
            this.service.GetVisitsForDashboardAsync();
        }

        private void FilterVisits()
        {
            this.VisitsToDisplay.Filter = null;
            this.VisitsToDisplay.Filter = this.SelectedFilter.Filter;
            //(v) =>
            //{
            //    return this.SelectedFilter.Filter(v);
            //};
        }

        protected void OnAdd()
        {
            NavigationHelper.NewVisit(this);
        }

        private void InitialzeFilters()
        {
            this.Filters = new ObservableCollection<DashboardFilterViewModel>();

            Filters.Add(new DashboardFilterViewModel(this));
            Filters.Add(new DashboardFilterValidViewModel(this, true));
            Filters.Add(new DashboardFilterValidViewModel(this, false));

            this.SelectedFilter = filters[0];
        }
        #endregion

        #region Events
        void OnGetVisitsForDashboardCompleted(ServiceResponseArgs<ReadOnlyCollection<IVisitDashboard>> e)
        {
            IsLoading = false;
            if (e.Error != null)
            {
                ChildWindow errorWin = new ErrorWindow(e.Error);
                errorWin.Show();
            }

            if (this.Visits != null)
            {
                foreach (VisitDashboardViewModel visit in this.Visits)
                {
                    visit.Cleanup();
                }
                this.Visits.Clear();
            }
            else
            {
                this.Visits = new ObservableCollection<VisitDashboardViewModel>();
            }

            foreach (IVisitDashboard visit in e.Result)
            {
                VisitDashboardViewModel vm = new VisitDashboardViewModel(visit);
                this.Visits.Add(vm);
            }

            VisitsToDisplay = new PagedCollectionView(this.Visits);

            InitialzeFilters();

            //FilterVisits();
        }


        #endregion

    }
}
